<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>H+ 后台主题UI框架 - 空白页</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="favicon.ico"> <link href="__STATIC__/layout/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="__STATIC__/layout/css/font-awesome.css?v=4.4.0" rel="stylesheet">
     <!-- jqgrid-->
    <link href="__STATIC__/layout/css/animate.css" rel="stylesheet">
    <link href="__STATIC__/layout/css/style.css?v=4.1.0" rel="stylesheet">
    <link href="__STATIC__/layout/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
    <link href="__STATIC__/layout/css/gyq.css" rel="stylesheet">
    <link rel="stylesheet" href="__STATIC__/layout/css/demo.css" type="text/css">
      <link rel="stylesheet" href="__STATIC__/layout/css/channel.css" type="text/css">
</head>

<body style="color:#333;padding:3px;" class="x_sorll">
    <!-- 标题 -->
    <div class="page-heade">
      <h2 class="title_color text-center">分组价格管理</h2>
    </div>
    <!-- 标题 -->
    <div class="row-margin-top">
    <form action="" id="f1"  class="form-inline">
      <input type="hidden" name="gid" value="{$gid}" id="gid"/>
      <table class="row-margin-top tab-content table-bordered  table" border="1">
          <tr>
            <td><label for="" class="form-label">分组编号：</label></td>
            <td>{$apply.group_no}</td>
            <td><label for="" class="form-label">分组名称：</label></td>
            <td class="font-bold">{$apply.group_name}</td>
            <td><label for="" class="form-label">包含经销商数：</label></td>
            <td class="font-bold"><a href="javascript:;" class="showAgencyInfo">{$apply.gysnum}</a></td>
             <td><label for="" class="form-label">包含单品数：</label></td>
            <td class="font-bold">{$apply.pronum}</td>
            </tr>
           <tr>
              <td colspan="8" class="text-left">
              	
                <div class="container-fluid">
                        <div class="row">
                          <div class="col-xs-3">请在此输入要新增的产品名称：</div>
                            <div class="col-xs-5">
                                <div class="input-group" >
                                    <input type="text" placeholder="请输入关键字或空格查询" class="form-control pro"  value="">
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                            <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                        </ul>
                                    </div>
                                </div>
                                <div class="selectedOnlyOne " style="display:none;" >
                                  
                                    <span class="inin"></span>
                                    <span class="close">×</span>
                                </div>
                            </div>
                          <div class="col-xs-4">
                              <button class="btn btn-success add" type="button">新增</button>
                              <button class="btn btn-danger delete" type="button">删除</button>
                              <button class="btn btn-info lendData" type="button">导入数据</button>
                    
                          </div>
                        </div>
                    </div>
            
            </td>
            </tr>
      </table>
      <table class="table table-striped table-bordered text-center" id="verifytable">
          <tr class="info text-center">
            <th style="vertical-align: middle !important;text-align: center;"><input type="checkbox" id="qx"></th>
            <th class="text-center">产品编码</th>
            <th class="text-center">产品名称</th>
            <th class="text-center">零售单位</th>
            <th class="text-center">零售供货价(元)</th>
            <th class="text-center">发运单位</th>
            <th class="text-center">发运供货价(元)</th>
            <th class="text-center">有效从</th>
            <th class="text-center">有效到</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
          </tr>
          <tbody id="tb">
          {volist name="applys" id="vo"}
              <tr>
                <input type="hidden" name="pid[]" value="{$vo.id}"/>
              		<td align="center"><input type="checkbox" value="{$vo.id}" class="dx"></td>
                    <td>{$vo.pro_no}</td>
                    <td>{$vo.pro_name}</td>
                    <td>{$vo.unit}</td>
                    <td><input type="text" class="form-control num lingshou" name="lingshou_price[]" value="{$vo.lingshou_price}" style="min-width:120px;width:120px;height: 26px;"></td>
                    <td>箱</td>
                    <td><input type="text" class="form-control num fayun" name="fayun_price[]" value="{$vo.fayun_price}" style="min-width:120px;width:120px;height: 26px;"></td>
                    <td> <input type="text" class="form-control start layer-date " style="min-width:120px;width:120px;height: 26px;display: inline-block;" name="start_time[]" value="{$vo.start_time}"></td>
                    <td> <input type="text" class="form-control end layer-date" style="min-width:120px;width:120px;height: 26px;display: inline-block;"  name="end_time[]" value="{$vo.end_time}"></td>
                    <td></td>
              </tr>
           {/volist}   
            </tbody>
            <tfoot>
              <tr class="delT">
					<td class="text-left" colspan="10"><button class="btn btn-danger" type="button" id="shanchu">删除</button></td>
              </tr>
         </tfoot>
      </table>
       <div class="text-center">
            <button class="btn btn-success" type="button" id="tijiao">提交</button>
            <button class="btn btn-info" style="margin-left: 20px" type="button" onclick="back()">返回</button>
      </div>
</form>
    </div>
    <!-- 全局js -->
    <script src="__STATIC__/layout/js/jquery.min.js?v=2.1.4"></script>
    <script src="__STATIC__/layout/js/bootstrap.min.js?v=3.3.6"></script>
    <!-- <script src="js/plugins/metisMenu/jquery.metisMenu.js"></script> -->
    <script src="__STATIC__/layout/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
    <script src="__STATIC__/layout/js/plugins/layer/layer.min.js"></script>
    <script src="__STATIC__/layout/js/plugins/sweetalert/sweetalert.min.js"></script>
    <script src="__STATIC__/layout/js/plugins/layer/laydate/laydate.js"></script>
     <script src="__STATIC__/layout/js/common.js"></script>
     <script src="__STATIC__/layout/js/plugins/suggest/bootstrap-suggest.min.js"></script>

    <script>
 
    //验证
    function yanzheng(){
       var flag=0;
      $(".lingshou").each(function(){
        if($(this).val()==""){
          flag=1;
          return false;
        }
        if($(this).val()<=0){
          flag=2;
          return false;
        }
      })
      if(flag==1){
        return "存在未填写的零售价";
      }else if(flag==2){
        return "零售价不能为0或负数";
      }
      flag=0;
       $(".fayun").each(function(){
        if($(this).val()==""){
          flag=1;
          return false;
        }
        if($(this).val()<=0){
          flag=2;
          return false;
        }
      })
      if(flag==1){
        return "存在未填写的发运价";
      }else if(flag==2){
        return "发运价不能为0或负数";
      }
      flag=0;
        $(".start").each(function(){
        if($(this).val()==""){
          flag=1;
          return false;
        }
      
      })
      if(flag==1){
        return "存在未填写的有效期开始时间";
      }
       flag=0;
        $(".end").each(function(){
        if($(this).val()==""){
          flag=1;
          return false;
        }
         
      })
      if(flag==1){
        return "存在未填写的有效期结束时间";
      }

      return "";
    }

    $("#tijiao").click(function(){
      var msg=yanzheng();
      if(msg==""){
        $.ajax({
                        cache: true,
                        type: "POST",
                        url:"{:url('pro_change')}",
                        data:$('#f1').serialize(),// 你的formid
                        async: false,
                        error: function(requests) {
                            alert("Connection error");
                        },
                        success:function(datas){
                          //alert(datas);
                          var res1=JSON.parse(datas);
                          if(res1["state"]==1){
                                      swal({
                              title: "完成",
                              text: "操作已成功",
                              type: "success"
                              },function(){
                               location.href="{:url('manage')}";
                               
                              });
                                    }else{
                                      swal({
                              title: "错误",
                              text:  res1["msg"],
                              type: "warning"
                              });
                          }
                        }
                       })
      }else{
          swal({
                title: "错误",
                text: msg,
                type: "warning"
           })
      }
    })
    $(".lendData").click(function(){
      location.href="{:url('daoruprice')}";
    })
    $(".delete").click(function(){
       if($(".pro").val()==""){
        swal({
                title: "错误",
                text: "您没有选择任何一款产品",
                type: "warning"
           })
      }else{
        var flag=0;var tr;
        $(".dx").each(function(){
          if($(this).val()==$(".pro").val()){
            flag=1;
            tr=$(this).parent().parent();
          }
        })
        if(flag==0){
            swal({
                title: "错误",
                text: "列表中不存在您选择的产品",
                type: "warning"
           })
        }else{
          //存在
          swal({
            title: "提示",
            text: "确定删除选中的产品吗？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#FF3333",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: true
              },function(isConfirm){
                  if (isConfirm) {
                       $.post("{:url('shanchupid')}",{pid:$(".pro").val(),gid:$("#gid").val()},function(re){
                   
                      if(re==1){
                layer.closeAll(); 
                       swal({
                              title: "完成",
                              text: "操作已成功",
                              type: "success"
                              },function(){
                                
                                tr.remove();
                              });
                      }else{
                        swal({
                              title: "错误",
                              text: "系统繁忙请稍后再试",
                              type: "warning"
                              })
                      }

                    })
                  }
              })
        }
      }
    })
    $(".add").click(function(){
      if($(".pro").val()==""){
        swal({
                title: "错误",
                text: "您没有选择任何一款产品",
                type: "warning"
           })
      }else{
        //判断是否存在
        var flag=0;
        $(".dx").each(function(){
          if($(this).val()==$(".pro").val()){
            flag=1;
          }
        })
        if(flag==1){
            swal({
                title: "错误",
                text: "不能重复添加相同产品",
                type: "warning"
           })
        }else{
          $.post("{:url('getpro')}",{pid:$(".pro").val()},function(re){
              var data=JSON.parse(re);
              var str='<tr> <input type="hidden" name="pid[]" value="'+data["id"]+'"/><td align="center"><input type="checkbox"  value="'+data["id"]+'" class="dx"></td><td>'+data["pro_no"]+'</td><td>'+data["pro_name"]+'</td><td>'+data["unit"]+'</td><td><input type="text" class="form-control num lingshou" name="lingshou_price[]" value="" style="min-width:120px;width:120px;height: 26px;"></td> <td>箱</td><td><input type="text" class="form-control num fayun" name="fayun_price[]" value="" style="min-width:120px;width:120px;height: 26px;"></td> <td> <input type="text" class="form-control start layer-date " style="min-width:120px;width:120px;height: 26px;display: inline-block;" name="start_time[]" value=""></td><td> <input type="text" class="form-control end layer-date" style="min-width:120px;width:120px;height: 26px;display: inline-block;"  name="end_time[]" value=""></td> <td></td></tr>';
              $("#tb").append(str);
                  //日期范围限制
        var starts = {
            elem: '#tb .start',
            format: 'YYYY-MM-DD',
            
            max: '2099-06-16 23:59:59', //最大日期
            istime: true,
            istoday: false,
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };
        var ends = {
            elem: '#tb .end',
            format: 'YYYY-MM-DD',
            
            max: '2099-06-16 23:59:59',
            istime: true,
            istoday: false,
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        laydate(starts);
        laydate(ends);
          })  
        }
      }
    })
   
        //供应商搜索自动补全
    $(".pro").bsSuggest({
        url: "{:url('projson')}",
        /*effectiveFields: ["userName", "shortAccount"],
         searchFields: [ "shortAccount"],*/
        effectiveFieldsAlias:{title: "名称"},
        ignorecase: true,
        showHeader: true,
        showBtn: false,     //不显示下拉按钮
        delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
        idField: "id",
        keyField: "title",
        clearable: true
    }).on('onSetSelectValue', function (e, keyword, data) {
        $(this).closest('.input-group').hide().next('.selectedOnlyOne').show().find('.inin').html(keyword.key)
        $(this).val(keyword.id)

    });
    $("#shanchu").click(function(){
      var gids=[];var i=0;
      $(".dx").each(function(){
        if($(this).prop("checked")){
          gids[i]=$(this).val();
          i++;
        }
      })
      if(i==0){
        swal({
                 title: "完成",
                 text: "至少选中一条要删除的数据",
                 type: "warning"
               })
      }else{
        var str=gids.join(',');
          swal({
            title: "提示",
            text: "确定删除选中的产品吗？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#FF3333",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: true
              },function(isConfirm){
                  if (isConfirm) {
                    //删除操作
                    $.post("{:url('shanchupid')}",{pid:str,gid:$("#gid").val()},function(re){
                   
                      if(re==1){
                layer.closeAll(); 
                       swal({
                              title: "完成",
                              text: "操作已成功",
                              type: "success"
                              },function(){
                                
                                location.href="{:url('prolist')}?gid="+$("#gid").val();
                              });
                      }else{
                        swal({
                              title: "错误",
                              text: "系统繁忙请稍后再试",
                              type: "warning"
                              })
                      }

                    })
                  }
                }

              )
      }
    })
    $("body").on("blur",".num",function(){
      if(isNaN(parseFloat($(this).val()))){
        $(this).val("");
      }else{
         $(this).val(parseFloat($(this).val()));
      }
    })
       //取消单选
    $('body').on('click','.selectedOnlyOne .close',function () {
        $(this).closest('.selectedOnlyOne').hide().find('.inin').html('')
        $(this).closest('.selectedOnlyOne').prev('.input-group').show().find('input').val('')
    })
       //全选
      $('body').on('change','.dx',function(){
            var flag=1;
        $(".dx").each(function(){
          if($(this).prop("checked")==false){
            flag=0;
          }
        })
        if(flag==1){
          $("#qx").prop("checked",true);
        }else{
          $("#qx").prop("checked",false);
        }
      })
    $("#qx").change(function(){
      if($(this).prop("checked"))
        $(".dx").prop("checked",true);
      else
        $(".dx").prop("checked",false);
    })
   
       $(".showAgencyInfo").click(function(){
          $.post("{:url('getjxslist')}",{gid:$("#gid").val()},function(re){
              var data=JSON.parse(re);
              var str='<form method="get" class="form-inline" style="margin:10px"><table class="table-bordered table"><tr><th colspan="2"  class="text-center font-bold info" style="font-size:20px">价格分组经销商详情</th></tr>';

              for(var i=0;i<data.length;i++){
                if(i==0){
                   str += '<tr><td colspan="1" style="width:160px">包含经销商</td><td colspan="4">'+data[i]["title"]+'</td></tr>'
                }else{
                  str += '<tr><td colspan="1" style="width:160px"></td><td colspan="4">'+data[i]["title"]+'</td></tr>'
                }
               
              }
              str+='</table></form>';
               layer.open({
                type: 1,
                title:"分组经销商详情",
                skin: 'layui-layer-rim', //加上边框
                area: ['800px', '540px'], //宽高
                content: str
              });

          })
          
           
        })
      function back(){
        location.href='{:url("manage")}';
      }
    	  //日期范围限制
        var start = {
            elem: '#tb .start',
            format: 'YYYY-MM-DD',
            
            max: '2099-06-16 23:59:59', //最大日期
            istime: true,
            istoday: false,
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };
        var end = {
            elem: '#tb .end',
            format: 'YYYY-MM-DD',
            
            max: '2099-06-16 23:59:59',
            istime: true,
            istoday: false,
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        laydate(start);
        laydate(end);
        $(document).ready(function(){
            // $("#verifytable tbody tr td").find('.duigou')
            // 

          /*格式化数字金额*/
          var $ObjectNum =$(".ObjectNum");
             toNum($ObjectNum);

/*新增*/
        $(".add").click(function() {
            // $("#alert_addNewZone").show();
          // console.log()
          var $addVal = $(".addVal").val();
            if ($addVal==="") {
                sweetAlert("", "请填写一个需要新增的产品！", "error");
            } else {
             return false
            }
        });


          /*table表格操作*/
      $("#checkAll").click(function() {

            $("[name=items]:checkbox").prop("checked", $(this).prop("checked"));
            if ($("[name=items]:checkbox").prop("checked") != true) {
                // console.log($(this))
                $("[name=items]:checkbox").parents("tr").css("backgroundColor", "#fff")
            } else {
                $("[name=items]:checkbox").parents("tr").css("backgroundColor", "#f8f800")

            }
        })
        $("[name=items]:checkbox").click(function() {
            //定义一个临时变量，避免重复使用同一个选择器选择页面中的元素，提高程序效率
            // console.log(1)
            var $tmp = $("[name=items]:checkbox");
            if ($(this).prop("checked") != true) {
                // console.log($(this))
                $(this).parent().parent().css("backgroundColor", "#fff")
            } else {
                $(this).parent().parent().css("backgroundColor", "#f8f800")

            }
            //用filter()方法筛选出选中的复选框，并直接给CheckedAll赋值
            $("#checkAll").prop('checked', $tmp.length == $tmp.filter(':checked').length);

          })
        })
         
    </script>
</body>

</html>
